<?php
	
	/**
	 * 判断输入的URL是否和用户当前登陆网站所使用的domain相同
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	function tools_isRefererEqual__($url){
		if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!="") {
			$url_array=parse_url($_SERVER['HTTP_REFERER']);
			if (strtoupper($url_array['host'])==strtoupper($url)) {
				return true;
			}else {
				return false;
			}
		}else{
			return false;
		}
	}

	/**
	 * 挑选变量,根据变量内容
	 *
	 * @return unknown
	 */
	function tools_pick_Value__(){
		$values_array=func_get_args();
		if (is_array($values_array[0])) {
			$values_array=$values_array[0];
		}
		
		foreach ($values_array as $value){
			if (isset($value) && strlen($value)>0) {
				return $value;
			}
		}
		
		return "";

	}
	
	/**
	 * 挑选变量,根据变量状态
	 *
	 * @return unknown
	 */
	function tools_pick_Var__(){
		$var_arr=func_get_args();

		foreach ($var_arr as $var){
			if (!empty($var)) {
				return $var;
			}
		}
		
		return null;
		
	}
	/**
	 * 页面跳转
	 *
	 * @param unknown_type $address
	 */
	function tools_forward($address){
		header("location: ".$address);
	}
	
	/**
	 * 得到当前域名(简单方法)
	 *
	 * @return unknown
	 */
	function tools_get_this_url__(){
		return $_SERVER['HTTP_HOST'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	}
	
	/**
	 * 判断输入的域名是否在系统定义的域名列表中
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	function tools_this_domain_token__($url=''){
		$url=($url==''?$_SERVER['HTTP_REFERER']:$url);
		$doname=parse_url($url);
		unset($url);
		$doname=$doname['host'];

		if (in_array($doname,explode('|',OTHER_DOMAIN)) || $doname==THIS_DOMAIN) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 生成随即字母和数字
	 *
	 * @param unknown_type $number
	 * @param unknown_type $being_caps
	 * @return unknown
	 */
	function tools_rand_str__($number,$being_caps=false){
		if (intval($number)<=0 || intval($number)>65535) {
			$number=rand(1,7);
		}		
		$type_max=($being_caps===true?3:2);
		
		$str='';
		for ($i=0;$i<$number;$i++){
			switch (rand(1,$type_max)){
				case 1:
					$str.=rand(0,9);
					break;
				case 2:
					$str.=chr(rand(97,122));
					break;
				case 3:
					$str.=chr(rand(65,90));
					break;

			}
		}

		return $str;
	}
	

	/**
	 * 分页函数
	 *
	 * @param Int $data_count 数据总条数
	 * @param Int $per_page_count 分页后每页的条数
	 * @param Int $now_page_num 当前页码
	 * @return Array array(0 "page_count"=>分页后页面总数,1 "now_first"=>当前页的第一条记录在数据库中的顺序号,2 "page_num"=>当前的页码,3 "data_count"=>记录总条数)
	 */
	function tools_get_navigat_interval__($data_count,$per_page_count,$now_page_num=1){
		//数据总条数,分页后每页的条数,当前页码
		$page_count=intval($data_count/$per_page_count);
		if ($data_count%$per_page_count!==0) {
			//总页数
			$page_count++;
		}
		if (empty($now_page_num)) {
			$now_page_num = 1;
		}else{
			$now_page_num=intval($now_page_num);
			if ($now_page_num<0) {
				$now_page_num = $page_count - (abs($now_page_num)-1);
			}
			if ($now_page_num<=0) {
				$now_page_num = 1;
			}
			if ($now_page_num>$page_count) {
				$now_page_num = $page_count;
			}
		}

		//本页第一条记录的记录号
		$now_page_first=($now_page_num-1)*$per_page_count;
		if ($now_page_first<0) {
			$now_page_first=0;
		}
		//0=>分页后页面总数，1=>当前页的第一条记录在数据库中的顺序号，2=>当前的页码，3=>记录总条数
		return array(0=>$page_count,'page_count'=>$page_count,
					 1=>$now_page_first,'now_first'=>$now_page_first,
					 2=>$now_page_num,'page_num'=>$now_page_num,
					 3=>$data_count,'data_count'=>$data_count);
	}
	
	/**
	 * 兼容版分页函数
	 *
	 * @param unknown_type $data_count
	 * @param unknown_type $per_page_count
	 * @param unknown_type $now_page_num
	 * @return unknown
	 */
	function tools_data_pagination__($data_count,$per_page_count,$now_page_num=1){
		return tools_get_navigat_interval__($data_count,$per_page_count,$now_page_num);
		
		
	}
	
	/**
	 * 不定长(射线)分页
	 *
	 * @param unknown_type $now_page_no
	 * @param unknown_type $file_name
	 * @param unknown_type $key_name
	 * @param unknown_type $other
	 * @return unknown
	 */
	function tools_get_navigat_ray__($now_page_no,$display_len,$previous_count){
		//导航数组,格式为 0,当前页码 1,左边开头 2右边结束 
		$navigat_arr = array();
		
		$now_page_no = (int)$now_page_no;
		$display_len = (int)$display_len;
		$previous_count = (int)$previous_count;
		
		if ($previous_count >= $display_len) {
			$previous_count = $display_len-1;
		}
		if ($previous_count<0) {
			$previous_count = 0;
		}
		
		if ($now_page_no<=0) { //如果当前页号码小于1
			
			$navigat_arr[0] = 1; //当前页
			$navigat_arr[1] = 1; //开始页码
			$navigat_arr[2] = $display_len; //结束页码
			
			/*$navigat_arr[1] = true;
			for ($i=2;$i<=$display_len;$i++){
				
				$navigat_arr[$i] = false;
			}*/
		}else if ($now_page_no <= $previous_count) {  //如果当前页号小于 显示长度-当前页号码,那么就显示1到$display_len个导航连接
			/**
			 *  对上面判断的解释:$now_page_no越接近$display_len的中心,这个条件的成立条件也越接近中心,
			 *  也就是说,如果$display_len是20 $now_page_no是8,那么上面的减法的结果就是12,no=7,运算=13,这里也可以用除法,但似乎在计算机中减法要比除法快,所以用了减法
			 */
			/*for ($i=1;$i<=$display_len;$i++){
				if ($i == $now_page_no) {
					$navigat_arr[$i] = true;
				}else{
					$navigat_arr[$i] = false;
				}
			}*/
			
			$navigat_arr[0] = $now_page_no; //当前页
			$navigat_arr[1] = 1; //开始页码
			$navigat_arr[2] = $display_len; //结束页码
			
			
			
		}else{
			
			/*$display_link_num = ($display_len-1)/2;
			if (strtoupper($toward) == 'L') {
				$display_link_L = floor($display_link_num);
				$display_link_R = ceil($display_link_num);				
			}else {
				$display_link_R = floor($display_link_num);
				$display_link_L = ceil($display_link_num);								
			}*/


			$end_no = $now_page_no + ($display_len-1-$previous_count);
			
			/*for ($i=$now_page_no - $display_link_L ; $i<=$end_no ;$i++){
				if ($i == $now_page_no) {
					$navigat_arr[$i] = true;
				}else{
					$navigat_arr[$i] = false;
				}
				
			}*/
			
			$navigat_arr[0] = $now_page_no; //当前页
			$navigat_arr[1] = $now_page_no - $previous_count; //开始页码
			$navigat_arr[2] = $end_no; //结束页码
			
			
			
		}
			$navigat_arr[0] = (int)$navigat_arr[0];
			$navigat_arr[1] = (int)$navigat_arr[1];
			$navigat_arr[2] = (int)$navigat_arr[2];
			
			return $navigat_arr;
			
	}
	
	/**
	 * 从SESSION中弹出验证码
	 *
	 * @return unknown
	 */
	function tools_pop_vision_identify_code_by_session__str(){
		@session_start();
		$v_code=$_SESSION[session_id()."_VISION_IDENTIFY_CODE"];
		unset($_SESSION[session_id()."_VISION_IDENTIFY_CODE"]);
		return $v_code;
	}
	
	/**
	 * 设置验证码到SESSION中
	 *
	 * @param unknown_type $vision_identify_code
	 */
	function tools_set_vision_identify_code_to_session_Str($vision_identify_code){
		@session_start();
		$_SESSION[session_id()."_VISION_IDENTIFY_CODE"]=$vision_identify_code;
	}
	
	/**
	 * 从SESSION中弹出验证码
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	function tools_pop_vision_identify_code_time_by_session__str($name=''){
		@session_start();
		if (empty($name)) {
			$name='validate_time';
		}
		$time=$_SESSION[session_id()."_VISION_IDENTIFY_CODE".$name];
//		var_dump($time);exit
		unset($_SESSION[session_id()."_VISION_IDENTIFY_CODE".$name]);
		return $time;
	}
	
	/**
	 * 设置验证码的生成时间到SESSION中
	 *
	 * @param unknown_type $time
	 * @param unknown_type $name
	 */
	function tools_set_vision_identify_code_time_to_session_Str($time,$name=''){
		@session_start();
		if (empty($name)) {
			$name='validate_time';
		}
		$_SESSION[session_id()."_VISION_IDENTIFY_CODE".$name]=$time;

	}

	/**
	 * 用于验证视觉验证码是否正确以及是否过期
	 *
	 * @param String $vi_code 用来验证的验证码
	 * @param String $life_time_second 最大生命时间
	 * @return Int or Boolean true:验证成功 , -1:验证码超时 , -2:验证码错误
	 */
	function tools_check_vi_code__($vi_code='',$life_time_second=''){
		if ($life_time_second==''){
			
			$life_time_second=CAPTCHA_LIFT_TIME_SECOND;
		}
		if ((time()-(tools_pop_vision_identify_code_time_by_session__str()))>$life_time_second) {
			return -1;//验证码超时
		}
		if (strtolower($vi_code)!=strtolower(tools_pop_vision_identify_code_by_session__str())) {
			return -2;//验证码错误
		}
		return true;
	}
	
	/**
	 * 设置SESSION中的一项
	 *
	 * @param unknown_type $name
	 * @param unknown_type $value
	 */
	function tools_set_session($name,$value){
		@session_start();
		$_SESSION[session_id()."_session_items_".$name]=serialize($value);
	}
	
	/**
	 * 从SESSION中得到一项
	 * 
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	function tools_get_session__($name){
		@session_start();
		return unserialize($_SESSION[session_id()."_session_items_".$name]);
	}
	
	/**
	 * 从SESSION中弹出一项
	 *
	 * @param unknown_type $name
	 * @param unknown_type $return
	 * @return unknown
	 */
	function tools_pop_session__($name,$return=true){
		@session_start();
		if ($return===true) {
			$item=unserialize($_SESSION[session_id()."_session_items_".$name]);
			unset($_SESSION[session_id()."_session_items_".$name]);
			return $item;
		}else{
			unset($_SESSION[session_id()."_session_items_".$name]);
		}
	}
	
	/**
	 * 判断SESSION中某项是否为空
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	function tools_is_empty_session__($name){
		@session_start();
		$temp=$_SESSION[session_id()."_session_items_".$name];
		return empty($temp);
	}
	
	/**
	 * 从SESSION中移除项目
	 *
	 * @param unknown_type $name
	 */
	function tools_remove_session($name){
		@session_start();
		unset($_SESSION[session_id()."_session_items_".$name]);
	}
	


?>